package com.shycloud.mido.homework.controller;

import com.shycloud.mido.homework.api.entity.WeekMain;
import com.shycloud.mido.homework.api.entity.WeekMemberSubscribe;
import com.shycloud.mido.homework.service.WeekMainService;
import com.shycloud.mido.homework.service.WeekMemberSubscribeService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.annotation.Inner;
import com.shycloud.mido.homework.thread.WeekAsyncTask;
import com.shycloud.mido.homework.thread.WeekSendAsyncTask;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author bard.bai
 */
@Slf4j
@Api(tags = "周报相关")
@RestController
@AllArgsConstructor
@RequestMapping("/week")
public class HomeworkWeekController {

	/** 周报任务 */
	private final WeekAsyncTask weekAsyncTask;
	/** 周报发送 */
	private final WeekSendAsyncTask weekSendAsyncTask;
	/** 周报订阅 */
	private final WeekMemberSubscribeService memberSubscribeService;
	/** 周报主表 */
	private final WeekMainService weekMainService;

	@Inner
	@ApiOperation("周报任务创建")
	@GetMapping("/weekJobBuild")
	public R<Boolean> weekJobBuild(String weekDate) throws Exception {
		LocalDateTime now = LocalDateTime.now();
		if (StringUtils.isNotBlank(weekDate)) {
			DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			now = LocalDateTime.parse(weekDate, dtf);
		}
		weekAsyncTask.asyncExecute(now);
		return new R<>(true);
	}

	@Inner
	@ApiOperation("周报消息推送")
	@GetMapping("/weekSendMember")
	public R<Boolean> weekSendMember() throws Exception {
		LocalDateTime now = LocalDateTime.now();
		weekSendAsyncTask.asyncExecute(now);
		return new R<>(true);
	}

//	@ApiOperation("周报订阅")
//	@GetMapping("/weekSubscribe")
//	public R weekSubscribe(String memberId, String studentId,) {
//		return new R(memberSubscribeService.save(WeekMemberSubscribe.builder()
//				.createTime(LocalDateTime.now())
//				.studentId(studentId)
//				.memberId(memberId)
//				.build()));
//	}

	@ApiOperation("获取周报信息")
	@GetMapping("/getWeekDetail")
	public R<WeekMain> getWeekDetail(String weekId) {
		return new R(weekMainService.getById(weekId));
	}

}
